import 'package:flutter/material.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterState createState() => new _RegisterState();
}

class _RegisterState extends State<RegisterPage> {
  GlobalKey<FormState> form = new GlobalKey<FormState>();
  String username;
  String password;
  void login() {
    var loginForm = form.currentState; // 读取当前的 Form 状态
    if (loginForm.validate()) {
      loginForm.save() ;
      print ('username :' + username + ' password : ' + password);
    }
  }
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Hello Flutter',
        home: new Scaffold(
            appBar: new AppBar(
                title: new Text ('Form 表单示例')
            ),
            body: new Column(
                children: <Widget>[
                  new Container(
                      padding: const EdgeInsets.all(16.0),
                      child: new Form(
                          key: form,
                          child: new Column(
                              children: <Widget>[
                                new TextFormField(
                                  decoration: new InputDecoration(
                                      labelText: '请输入用户名'),
                                  onSaved: (value) {
                                    username = value;
                                  },
                                  onFieldSubmitted: (value) {},
                                ),
                                new TextFormField(
                                  decoration: new InputDecoration(
                                      labelText: '请输入密码'),
                                  obscureText: true,
                                  validator: (value) { return value.length < 6 ? '密码长度不够 6 位' : null; },
                                  onSaved: (value) {
                                    password = value;
                                  },
                                ),
                              ]
                          )
                      )
                  ),
                  new SizedBox(
                      width: 340.0,
                      height: 42.0,
                      child: new RaisedButton(
                          onPressed: login,
                          child: new Text(
                              '提交',
                              style: TextStyle(fontSize: 18.0,)
                          )
                      )
                  )
                ]
            )
        )
    );
  }
}



